package com.explorex.infra.puma.gateway.netty.ws.message.handler;


import com.explorex.infra.puma.gateway.netty.ws.WebSocketSession;
import com.explorex.infra.puma.gateway.netty.ws.enums.MessageTypeEnum;
import com.explorex.infra.puma.gateway.netty.ws.message.Message;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageBody;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageHeader;
import com.explorex.infra.puma.gateway.netty.ws.message.RespMessageBody;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: JacceYang
 * @createTime: 2022/07/15 1:58 下午
 * @description:
 */
public class PingMessageProcessor implements BaseProcessor {

    @Override
    public short getMessageType() {
        return MessageTypeEnum.PING.getType();
    }

    @Override
    public Message process(WebSocketSession session, Message messageFrame) {
        MessageHeader respHeader = new MessageHeader(MessageTypeEnum.PONG.getType());
        respHeader.trackHeader(messageFrame.getHeader());
        Message message = new Message();
        message.setHeader(respHeader);
        message.setBody(new PingMessageAck());
        session.refresh(true);
        return message;
    }

    @Override
    public MessageBody parseBody(int contentType, String msgBody) {
        return new PingMessage();
    }

    @Data
    static class PingMessage implements MessageBody, Serializable {
    }

    @Data
    static class PingMessageAck extends RespMessageBody implements Serializable {
    }
}
